package com.lepu.app.fun.hospital;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.app.application.MyApplication;
import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.widget.GoogleRefreshLayout;
import com.eyzhs.app.R;
import com.lepu.app.fun.hospital.adapter.LectureListAdapter;
import com.lepu.app.fun.knowledge.bean.Article;
import com.lepu.app.main.activity.AppBroswerActivity;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class HospitalLectureActivity extends BaseFragmentActivity implements
        OnTopbarNewLeftLayoutListener, AsyncRequest, GoogleRefreshLayout.OnRefreshListener, GoogleRefreshLayout.OnLoadListener {
    private static final String REQUEST_DATA = "request_data";
    private static final String REQUEST_DATA_MORE = "request_data_more";
    public final int pageSize = 25;
    //    public final int pageSize = 15;
    private ArrayList<Article> mArticleList = new ArrayList<Article>();
    private LectureListAdapter mAdapter;
    private GoogleRefreshLayout mGoogleRefreshLayout;
    private int mCurrentPage = 1;
    private ListView mListview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mother_hospital_lecture_activity);

        init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 添加页面动画，需要重写这个方法，
     */
    @Override
    public void onBackPressed() {
        finish(true);
    }

    /**
     * 初始化方法
     */
    @SuppressLint("SimpleDateFormat")
	@SuppressWarnings("deprecation")
    private void init() {
        // topbar
        CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
        topbar.setTopbarTitle("健康讲座");
        topbar.setonTopbarNewLeftLayoutListener(this);

        mGoogleRefreshLayout = (GoogleRefreshLayout) findViewById(R.id.swipeLayout);
        mGoogleRefreshLayout.setOnRefreshListener(this);
        mGoogleRefreshLayout.setOnLoadListener(this);
        mGoogleRefreshLayout.setColorScheme(R.color.color_listview_refresh_1,
                R.color.color_listview_refresh_2,
                R.color.color_listview_refresh_3,
                R.color.color_listview_refresh_4);
//        mGoogleRefreshLayout.setRefreshing(true);
        showFirstLoadDataLayout(true);
        request(false);

        mListview = (ListView) findViewById(R.id.mListview);
        mAdapter = new LectureListAdapter(this, mArticleList);

        ImageView tempImageView = new ImageView(HospitalLectureActivity.this);
        mListview.addHeaderView(tempImageView);
        mListview.setAdapter(mAdapter);
        mListview.removeHeaderView(tempImageView);

//        mListview.setAdapter(mAdapter);
        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Article article = mArticleList.get(i);
                if (article != null) {
                    try {
                        Intent intent = new Intent(HospitalLectureActivity.this, AppBroswerActivity.class);
                        String data1 = "<body style=\"background-color: transparent; font:18px Heiti\"><h3 align=\"center\" style=\"font-weight:bold;color:#20201e;\">";
                        String data2 = "</h3><center style=\\\"color:#5a5a5a;padding-bottom:0.5em;\\\">";
                        String data3 = "</center>";
                        String data4 = "</body>";
                        intent.putExtra("title", article.ArticleTitle);
                        SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd");
                        String time = DateUtilBase.stringFromDate(formart.parse(article.PublishDate), DateUtilBase.YEAR_MONTH_DAY);
                        String content = data1 + article.ArticleTitle + data2 + time + data3 + article.ArticleContent + data4;
                        intent.putExtra("load_data", content);
                        intent.putExtra("need_share", true);
                        intent.putExtra("ObjectType", 6);//讲座分享 
                        intent.putExtra("ObjectID", article.ArticleID);
                        intent.putExtra("info",article.ArticleDescription);
                        intent.putExtra("share_url",Setting.getAppShareArticleUrl(article.ArticleID));
                        startActivity(intent, true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });

    }

    @Override
    public void onTopbarLeftLayoutSelected() {
        finish(true);
    }


    public void request(boolean isMore) {
        int page = 1;
        String request = "";
        if (isMore) {
            request = REQUEST_DATA_MORE;
            page = (mCurrentPage + 1);
        } else {
            request = REQUEST_DATA;
            mCurrentPage = page;
        }

        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject params = new JSONObject();
        try {
            params.put("DeviceID", Setting.DEVICEID);
            params.put("ApplicationID", Setting.APPLICATIONID);
            params.put("UserID", LoginUtil.getUserId());
            params.put("LoginToken", LoginUtil.getLoginToken());
            params.put("ACIDList", "17");
            params.put("PageIndex", page);
            params.put("PageSize", pageSize);
            params.put("RefreshTime", DateUtilBase.stringFromDate(new Date(),
                    DateUtilBase.DATE_ALL_ALL));
            params.put("SortDirection", "DESC");

        } catch (Exception e) {
        }
        map.put("ht", params);

        ApiClient.http_post_main(Setting.getArticleListUrl(), map, null, this, request, true, false, true);

    }

    @Override
    public void RequestComplete(Object object, Object data) {
        ArrayList<Article> articleList = Article.parseJsonData((String) data);
        if (object.equals(REQUEST_DATA)) {
            showFirstLoadDataLayout(false);
            mGoogleRefreshLayout.setRefreshing(false);

            if (articleList != null && articleList.size() > 0) {
                showHideNoDataLayout(false);
                mArticleList.clear();
                mArticleList.addAll(articleList);
                mAdapter.notifyDataSetChanged();
            } else {
                showHideNoDataLayout(true);
            }
        } else {
            mGoogleRefreshLayout.setLoading(false);

            if (articleList != null && articleList.size() > 0) {
                mCurrentPage++;
                showHideNoDataLayout(false);
                mArticleList.addAll(articleList);
                mAdapter.notifyDataSetChanged();
            } else {
                UIHelper.showToast(MyApplication.getInstance(), R.string.no_data_more);
                mGoogleRefreshLayout.setLoadMoreFinish(true);
            }
        }
    }

    @Override
    public void RequestError(Object object, int i, String s) {
        if (object.equals(REQUEST_DATA)) {
            showFirstLoadDataLayout(false);
            showHideNoDataLayout(true);
            mGoogleRefreshLayout.setRefreshing(false);
        } else {
            mGoogleRefreshLayout.setLoading(false);
        }
    }

    @Override
    public void onLoad() {
        request(true);
    }

    @Override
    public void onRefresh() {

        request(false);
    }

    private void showHideNoDataLayout(boolean isShow) {
        LinearLayout noDataLayout = (LinearLayout) findViewById(R.id.noDataLayout);
        if (noDataLayout != null) {
            if (isShow) {
                if (mArticleList == null || mArticleList.size() == 0) {
                    noDataLayout.setVisibility(View.VISIBLE);
                }
            } else {
                noDataLayout.setVisibility(View.GONE);
            }
        }
    }

    private void showFirstLoadDataLayout(boolean isShow) {

        LinearLayout noDataLayout = (LinearLayout) findViewById(R.id.noDataLayout);
        noDataLayout.setVisibility(View.GONE);

        LinearLayout firstLoadDataLayout = (LinearLayout) findViewById(R.id.firstLoadDataLayout);
        if (firstLoadDataLayout != null) {
            if (isShow) {
                firstLoadDataLayout.setVisibility(View.VISIBLE);
            } else {
                firstLoadDataLayout.setVisibility(View.GONE);
            }
        }
    }
}
